const express =require("express");
const router=express.Router();
const {Stu} =require("../model/model");

//查询所有
router.get("/findStuAll",(req,res)=>{
    Stu.find({},function (err,stu) {
    if(err){
        console.log("没有对应数据！",+err);
    }else{
        res.send({
            code:200,
            msg:"查询成功！",
            result:stu
        })
    }
})
});

//按id修改
router.post("/updStu/:id",(req,res)=>{
    let obj={
        name:req.body.name,
        age:req.body.age,
        sex:req.body.sex,
        tel:req.body.tel,
        address:req.body.address,
        identity:req.body.identity
    };
Stu.updateMany({_id:req.params.id},{$set:obj},function (err,stu) {
    if(err){
        console.log("修改失败")
    }else{
        res.send({
            code:200,
            msg:"修改成功！",
            result:stu
        })
    }
})
});
//增加数据
router.post("/addStu",(req,res)=>{
    new Stu(req.body).save(function(err,stu){
        if(err){
            res.send({
                code:-1,
                msg:'数据库连接错误'
            })
        }else{
            res.send({
                code:200,
                msg:'添加学生信息成功',
                data:{
                    stu:stu
                }
            })
        }
    })
});
//删除数据
router.post("/delStu/:id",(req,res)=>{
    new Stu({_id:req.params.id}).remove(function(err,stu){
        if(err){
            res.send({
                code:-1,
                msg:'数据库连接错误'
            })
        }else{
            res.send({
                code:200,
                msg:'ok',
                data:{
                    stu:stu
                }
            })
        }
    })
});
module.exports=router;